<?php


namespace app\common\model;


use think\model\concern\SoftDelete;
use think\Db;

class NoticeUser extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_notice_user';
    // 主键ID
    protected $pk = 'id';


    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['id' => $id];
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 新增数据
            $this->allowField(true)->save($params);
            $rs = $this->id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }


    // 获取分页列表
    function get_list_page($cond,$orderby,$item,$limit)
    {
        $where = null;

        if(!empty($cond['shop_id'])){
            $where[] = ['a.shop_id', '=',$cond['shop_id']];
        }
        if(!empty($cond['user_id'])){
            $where[] = ['a.user_id', 'in',"0,".$cond['user_id']];
        }
        if(!empty($cond['shop_name'])){
            $where[] = ['c.shop_name','like',"%".$cond['shop_name']."%"];
        }

        $where[] = ['a.is_del','=',1];

        $rs = $this
            ->alias('a')
            ->leftJoin('ddt_shops c','a.shop_id = c.shop_id')
            ->leftJoin('ddt_user u','a.user_id = u.user_id')
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);



        return $rs;
    }
}